import AV from "leancloud-storage";

const appId = 'o7gkcpWvDsEywOvDS6G0QwTf-gzGzoHsz';
const appKey = 'hAsx98AMiq2d6B3yRbeeFhky';
const masterKey = 'WNpqkTm0RcDhhbmYCpfNNl9B';

AV.init({
  appId,
  appKey,
  masterKey
  // 启用美国节点
  // region: 'us',
});


export function addOnePostByAdmin(postData){
  let Post = AV.Object.extend("_Post");
  let post = new Post();
  post.set("postname", postData.postname);
  post.set("password", postData.password);
  post.set("email", postData.email);

  return post.save(null)
}

export function getPostById(id){
  let query = new AV.Query('_Post');
  return query.get(id);
}

export function destoryPost(id){
  // console.log(id);
  return getPostById(id).then(function(post){
    return post.destroy();
  }, function(error){
    console.log(error);
  });
}

export function fetchPosts(condition){

  let query = new AV.Query('_Post');

  query.limit(condition.limit);
  query.skip(condition.skip);

  query.descending(condition.descending);
  return query.find();
  //  .then(function(posts){
  //    console.log(posts);
  //  }, function(errors){
  //    console.log(errors);
  //  });
}
